﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using MyWeb.BusinessLogicLayer;
using System.IO;

public partial class User_Exam : System.Web.UI.Page
{

    Datacon dataconn = new Datacon();

    protected void Page_Load(object sender, EventArgs e)
    {

        ImageButton1.Attributes.Add("OnClick", "javascript:return confirm('确实要交卷吗？')");
        ImageButton2.Attributes.Add("OnClick", "javascript:return confirm('确实退出吗？')");
        if (!IsPostBack)
        {

            InitData();

            Label30.Text = Session["PaperName"].ToString();
            Label31.Text = Session["PaperID"].ToString();
            SqlConnection cn = dataconn.getcon();
            cn.Open();
            string sqlstr1 = "select Score from TaoTi where PaperID='" + Session["PaperID"] + "'";
            SqlCommand cmd1 = new SqlCommand(sqlstr1, cn);
            Label34.Text = cmd1.ExecuteScalar().ToString();
            string sqlstr2 = "select JoinTime from TaoTi where PaperID='" + Session["PaperID"] + "'";
            SqlCommand cmd2 = new SqlCommand(sqlstr2, cn);
            Label33.Text = cmd2.ExecuteScalar().ToString();
            string sqlstr3 = "select AnswerTime from TaoTi where PaperID='" + Session["PaperID"] + "'";
            SqlCommand cmd3 = new SqlCommand(sqlstr3, cn);
            Label32.Text = cmd3.ExecuteScalar().ToString();
            cn.Close();
        }
    }
    protected void InitData()
    {
        Datacon DB = new Datacon();
        int paperID = int.Parse(Session["PaperID"].ToString());



        SqlParameter[] Params1 = new SqlParameter[2];
        Params1[0] = DB.MakeInParam("@PaperID", SqlDbType.Int, 4, paperID);               //试卷编号
        Params1[1] = DB.MakeInParam("@Type", SqlDbType.VarChar, 10, "单选题");            //题目类型        
        DataSet ds1 = DB.GetDataSet("Proc_PaperDetail", Params1);
        GridView1.DataSource = ds1;
        GridView1.DataBind();
        if (ds1.Tables[0].Rows.Count > 0)
        {
            ((Label)GridView1.HeaderRow.FindControl("Label27")).Text = ((Label)GridView1.Rows[0].FindControl("Label4")).Text;
        }

        SqlParameter[] Params2 = new SqlParameter[2];
        Params2[0] = DB.MakeInParam("@PaperID", SqlDbType.Int, 4, paperID);               //试卷编号
        Params2[1] = DB.MakeInParam("@Type", SqlDbType.VarChar, 10, "多选题");            //题目类型        
        DataSet ds2 = DB.GetDataSet("Proc_PaperDetail", Params2);
        GridView2.DataSource = ds2;
        GridView2.DataBind();
        if (ds2.Tables[0].Rows.Count > 0)
        {
            ((Label)GridView2.HeaderRow.FindControl("Label28")).Text = ((Label)GridView2.Rows[0].FindControl("Label8")).Text;
        }
        SqlParameter[] Params3 = new SqlParameter[2];
        Params3[0] = DB.MakeInParam("@PaperID", SqlDbType.Int, 4, paperID);               //试卷编号
        Params3[1] = DB.MakeInParam("@Type", SqlDbType.VarChar, 10, "判断题");            //题目类型        
        DataSet ds3 = DB.GetDataSet("Proc_PaperDetail", Params3);
        GridView3.DataSource = ds3;
        GridView3.DataBind();
        if (ds3.Tables[0].Rows.Count > 0)
        {
            ((Label)GridView3.HeaderRow.FindControl("Label29")).Text = ((Label)GridView3.Rows[0].FindControl("Label12")).Text;
        }
        SqlParameter[] Params4 = new SqlParameter[2];
        Params4[0] = DB.MakeInParam("@PaperID", SqlDbType.Int, 4, paperID);               //试卷编号
        Params4[1] = DB.MakeInParam("@Type", SqlDbType.VarChar, 10, "问答题");            //题目类型        
        DataSet ds4 = DB.GetDataSet("Proc_PaperDetail", Params4);
        GridView4.DataSource = ds4;
        GridView4.DataBind();
        if (ds4.Tables[0].Rows.Count > 0)
        {
            ((Label)GridView4.HeaderRow.FindControl("Label52")).Text = ((Label)GridView4.Rows[0].FindControl("Label57")).Text;
        }

    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            string PaperID = Session["PaperID"].ToString();
            string USERID = Session["ID"].ToString();

            SqlConnection cn = dataconn.getcon();
            cn.Open();
            string sqlUserDetail = string.Empty;
            int score = 0;
            int tsinglemark = 0;
            if (GridView1.Rows.Count > 0)
            {
                int singlemark = int.Parse(((Label)GridView1.Rows[0].FindControl("Label4")).Text);//取出单选题的每题分值
                foreach (GridViewRow dr in GridView1.Rows)//对单选题每题进行判断用户选择答案
                {
                    string str = "";
                    string ANSWER = ((Label)dr.FindControl("Label3")).Text.Trim();
                    string Mark = ((Label)dr.FindControl("Label4")).Text.Trim();
                    int UserMark = 0;
                    if (((RadioButton)dr.FindControl("RadioButton1")).Checked)
                    {
                        str = "A";
                    }
                    else if (((RadioButton)dr.FindControl("RadioButton2")).Checked)
                    {
                        str = "B";
                    }
                    else if (((RadioButton)dr.FindControl("RadioButton3")).Checked)
                    {
                        str = "C";
                    }
                    else if (((RadioButton)dr.FindControl("RadioButton4")).Checked)
                    {
                        str = "D";
                    }
                    if (ANSWER == str)//将用户选择结果和答案进行比较
                    {
                        tsinglemark = tsinglemark + singlemark;
                        UserMark = singlemark;
                    }

                    string TitleID = ((Label)dr.FindControl("Label13")).Text.Trim();
                    sqlUserDetail += string.Format(@"insert into USERAnsDetail (PaperID,Type,TitleID,[USERID],USERANSWER,ANSWER,Mark,UserMark) values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')  ",
                                                                            PaperID, "单选题", TitleID, USERID, str, ANSWER, Mark,UserMark);

                }
            }
            int tmultimark = 0;

            if (GridView2.Rows.Count > 0)
            {
                int multimark = int.Parse(((Label)GridView2.Rows[0].FindControl("Label8")).Text);//取出多选题每题分值
                foreach (GridViewRow dr in GridView2.Rows)//对多选题每题进行判断用户选择答案
                {
                    string str = "";
                    string ANSWER = ((Label)dr.FindControl("Label7")).Text.Trim();
                    string Mark = ((Label)dr.FindControl("Label8")).Text.Trim();
                    int UserMark = 0;
                    if (((CheckBox)dr.FindControl("CheckBox1")).Checked)
                    {
                        str += "A";
                    }
                    if (((CheckBox)dr.FindControl("CheckBox2")).Checked)
                    {
                        str += "B";
                    }
                    if (((CheckBox)dr.FindControl("CheckBox3")).Checked)
                    {
                        str += "C";
                    }
                    if (((CheckBox)dr.FindControl("CheckBox4")).Checked)
                    {
                        str += "D";
                    }
                    if (ANSWER == str)//将用户选择结果和答案进行比较
                    {
                        tmultimark = tmultimark + multimark;
                        UserMark = multimark;
                    }
                    string TitleID = ((Label)dr.FindControl("Label60")).Text.Trim();
                    sqlUserDetail += string.Format(@"insert into USERAnsDetail (PaperID,Type,TitleID,[USERID],USERANSWER,ANSWER,Mark,UserMark) values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')  ",
                                                                            PaperID, "多选题", TitleID, USERID, str, ANSWER,Mark, UserMark);


                }
            }
            int tjudgemark = 0;
            if (GridView3.Rows.Count > 0)
            {
                int judgemark = int.Parse(((Label)GridView3.Rows[0].FindControl("Label12")).Text);//取出判断题每题分值

                foreach (GridViewRow dr in GridView3.Rows)//对判断题每题进行判断用户选择答案
                {

                    string USERANSWER = ((RadioButtonList)dr.FindControl("RadioButtonList1")).SelectedValue;
                    string ANSWER = ((Label)dr.FindControl("Label11")).Text.Trim();
                    string Mark = ((Label)dr.FindControl("Label12")).Text.Trim();
                    int UserMark = 0;
                    bool j;
                    if (USERANSWER == "true")
                    {
                        j = true;
                        if (j == bool.Parse(ANSWER))
                        {
                            tjudgemark = tjudgemark + judgemark;
                            UserMark = judgemark;
                        }
                    }
                    if (USERANSWER == "false")
                    {
                        j = false;
                        if (j == bool.Parse(ANSWER))
                        {
                            tjudgemark = tjudgemark + judgemark;
                            UserMark = judgemark;
                        }
                    }
                    string TitleID = ((Label)dr.FindControl("Label14")).Text.Trim();
                    sqlUserDetail += string.Format(@"insert into USERAnsDetail (PaperID,Type,TitleID,[USERID],USERANSWER,ANSWER,Mark,UserMark) values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')  ",
                                                                            PaperID, "判断题", TitleID, USERID, USERANSWER, ANSWER,Mark, UserMark);

                }
            }


            if (GridView4.Rows.Count > 0)//上传
            {

                foreach (GridViewRow dr in GridView4.Rows)
                {
                    FileUpload fd = (FileUpload)dr.FindControl("myupload");
                    string Mark = ((Label)dr.FindControl("Label57")).Text.Trim();
                    if (fd.HasFile)
                    {
                        string filePath = Server.MapPath("~/UploadFiles/" + Session["NAME"] + "/");
                        if (!Directory.Exists(filePath))
                        {
                            Directory.CreateDirectory(filePath);
                        }
                        string fileName = fd.PostedFile.FileName;
                        //string[] sArray = fd.PostedFile.FileName.Split('.');
                        //string type = sArray[1];
                        //string fileName = sArray[0];
                        string pre = DateTime.Now.ToString("yyyyMMdd") + Guid.NewGuid().ToString();
                        fileName = pre + "_" + fileName;
                        fd.PostedFile.SaveAs(filePath + fileName);
                        string TitleID = ((Label)dr.FindControl("Label15")).Text.Trim();
                        string USERANSWER = "/UploadFiles/" + Session["NAME"] + "/" + fileName;
                        sqlUserDetail += string.Format(@"insert into USERAnsDetail (PaperID,Type,TitleID,[USERID],USERANSWER,Mark) values ('{0}','{1}','{2}','{3}','{4}','{5}')  ",
                                                                                PaperID, "问答题", TitleID, USERID, USERANSWER, Mark);

                    }
                }

            }
            SqlCommand cmd1 = new SqlCommand(sqlUserDetail, cn);
            cmd1.ExecuteScalar();
            score = tjudgemark + tmultimark + tsinglemark;
            Scores insertScore = new Scores();  //创建Scores类对象
            insertScore.UserID = Session["ID"].ToString();//设置Scores对象的属性
            insertScore.PaperID = int.Parse(Session["PaperID"].ToString());
            insertScore.Score = score;
            insertScore.sscore = tsinglemark;
            insertScore.mscore = tmultimark;
            insertScore.jscore = tjudgemark;
            insertScore.InsertByProc();//调用InsertByProc方法向数据库中插入成绩
            Response.Write("<script>alert('您交卷成功')</script>");
            Response.Redirect("Score.aspx");
            cn.Close();
        }
        catch (Exception ex)
        {
            Response.Write(ex);
        }



    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {


        Response.Redirect("ChoosePaper.aspx");

    }





}